/**
 * Copyright (C), 2011-2017
 * File Name: DateUtils.java
 * Encoding: UTF-8
 * Date: 17-9-5 下午6:32
 * History:
 */
package com.swak.frame.util;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;

import com.swak.frame.logger.SwakLoggerFactory;

/**
 * 
 * ClassName: DateTimeUtils.java
 * 
 * @Description: 时期处理类
 * @author colley.ma
 * @date 2021年3月15日
 */
public class DateTimeUtils {
	private static Logger logger = SwakLoggerFactory.getLogger(DateTimeUtils.class);

	public static Date string2Date(String strtime, String datepattern) {
		if (StringUtils.isEmpty(strtime)) {
			return null;
		}
		try {
			DateTimeFormatter format = DateTimeFormat.forPattern(datepattern);
			DateTime dateTime = DateTime.parse(strtime, format);
			return dateTime.toDate();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return null;
	}

	public static Date string2Date(String strtime) {
		int index = strtime.indexOf(":");
		String datepattern = null;
		if (index != -1) {
			datepattern = DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern;
		} else {
			datepattern = DFormatEnum.YYYY_MM_DD.pattern;
		}
		return string2Date(strtime, datepattern);
	}

	public static Calendar string2Calendar(String strtime) {
		Date d = string2Date(strtime);
		if (d != null) {
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			return c;
		}
		return null;
	}

	public static Timestamp string2Timestamp(String strtime, String datepattern) {
		Date date = string2Date(strtime, datepattern);

		if (date != null) {
			return new Timestamp(date.getTime());
		}

		return null;
	}

	public static String calendar2String(Calendar time) {
		return date2String(time.getTime());
	}

	public static String date2String(Date time, String datepattern) {
		if (StringUtils.isEmpty(datepattern)) {
			datepattern = DFormatEnum.DATA_FORMAT_DEFAULT;
		}
		return new DateTime(time).toString(datepattern);
	}

	public static String date2String(Timestamp time, String datepattern) {
		if (StringUtils.isEmpty(datepattern)) {
			datepattern = DFormatEnum.DATA_FORMAT_DEFAULT;
		}
		return new DateTime(time.getTime()).toString(datepattern);
	}

	public static String date2String(Date time) {
		return date2String(time, null);
	}

	public static String date2String(Timestamp time) {
		return date2String(time, null);
	}

	public static String sysTimeStr(String datepattern) {
		return date2String(new Date(), datepattern);
	}

	public static String sysTimeStr() {
		return sysTimeStr(null);
	}

	public static String YYYYMMDD2String() {
		return YYYYMMDD2String(new Date());
	}

	public static String YYYYMMDD2String(Date date) {
		return date2String(date, DFormatEnum.YYYYMMDD.pattern);
	}

	public static String fullDate2String(Date date) {
		return date2String(date, DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern);
	}

	public static String formateShortDate(Date date) {
		return date2String(date, DFormatEnum.YYYY_MM_DD.pattern);
	}

	public static String getShortDateStr() {
		return date2String(new Date(), DFormatEnum.YYYY_MM_DD.pattern);
	}

	public static Date getLastTimeDay(Date date) {
		String dateTemp = date2String(date, DFormatEnum.YYYY_MM_DD.pattern);
		return string2Date(dateTemp + " 23:59:59");
	}

	/**
	 * 获取在给定日期的基础上往前/后的第几周
	 *
	 * @param date 给定日期
	 * @param gap  移动的周数
	 * @return 年+周 例如 201532,2015年的第32周
	 */
	public static int weekOfYear(Date date, Integer gap) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.WEEK_OF_YEAR, gap);

		return Integer.valueOf(
				String.valueOf(calendar.get(Calendar.YEAR)) + String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR)));
	}

	/**
	 * 获取当前系统时间
	 *
	 * @return
	 */
	public static Timestamp getCurrentTime() {
		return new Timestamp(System.currentTimeMillis());
	}

	/**
	 * 将日期中的时间重置为0
	 * 
	 * @param date
	 * @return
	 */
	public static Date resetTime2Zero(Date date) {
		if (date == null) {
			return date;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
}
